//
//  DownloadViewController.m
//  JZMCFM
//
//  Created by Mc on 15/10/30.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "DownloadViewController.h"
#import "VideoDownLoadManager.h"
#import "DownLoadCell.h"
@interface DownloadViewController ()<UITableViewDataSource,UITableViewDelegate,VideoDownLoadManagerDelegate,DownLoadCellDelegate>
@property (strong, nonatomic) IBOutlet UITableView *mainTableView;

@end

@implementation DownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [VideoDownLoadManager sharedInstance].delegate = self;
    
    // 注册新的下载通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMovieDidStatrtDownAction:) name:k_newVideoDidStartDown object:nil];
    // 注册下载完成的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidFinishedSuccess:) name:k_videoDidDownedFinishedSuccess object:nil];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.mainTableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [VideoDownLoadManager sharedInstance].downVideoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownLoadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"down" forIndexPath:indexPath];
    DetailModel2 *model = [VideoDownLoadManager sharedInstance].downVideoArray[indexPath.row];
    [cell setCellDataWithModel:model];
    cell.delegate = self;
    return cell;
}

- (void)videoDidUpdatedProgressWithVideoModel:(DetailModel2 *)model
{
    NSMutableArray *mutableArray = [VideoDownLoadManager sharedInstance].downVideoArray;
    if ([mutableArray containsObject:model]) {
        NSInteger index = [mutableArray indexOfObject:model];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
        DownLoadCell *cell = (DownLoadCell *)[self.mainTableView cellForRowAtIndexPath:indexPath];
        
        if ([[self.mainTableView visibleCells] containsObject:cell]) {
            [cell updateDownLoadProgress:model];
        }
    }
}

// 新的音频开始下载通知
- (void)newMovieDidStatrtDownAction:(NSNotification *)sender
{
    [self.mainTableView reloadData];
}

// 下载完成
- (void)videoDidFinishedSuccess:(NSNotification *)sender
{
    DetailModel2 *model = sender.object;
    NSInteger index = [[VideoDownLoadManager sharedInstance].downVideoArray indexOfObject:model];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    DownLoadCell *cell = (DownLoadCell *)[self.mainTableView cellForRowAtIndexPath:indexPath];
    
    // 设置cell上的下载按钮为完成状态
    cell.palseAndPlayButton.selected = NO;
    [cell.palseAndPlayButton setImage:[UIImage imageNamed:@"iconfont-bofang"] forState:UIControlStateNormal];
    cell.palseAndPlayButton.userInteractionEnabled = NO;
}

- (void)MEDownLoadCellpalseAndPlayButtonDidClicked:(DownLoadCell *)cell withIsPlay:(BOOL)isPlay
{
    NSIndexPath *indexPath = [self.mainTableView indexPathForCell:cell];
    DetailModel2 *model = [VideoDownLoadManager sharedInstance].downVideoArray[indexPath.row];
    if (isPlay) {
        [[VideoDownLoadManager sharedInstance] downloadPausewithModel:model];
    }else{
        [[VideoDownLoadManager sharedInstance] downloadResumeWithModel:model];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
